package com.yudaob2b.elong.service;

import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.utils.IdGeneratorUtil;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.entity.dto.ELaElongHotelInvDTO;
import com.yudaob2b.elong.entity.vo.Inventory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 库存数据服务
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/15/18 8:54 PM
 */
@Service("hotelInventoryService")
public class HotelInventoryService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * Inventory转换ElongHotelInventoryPO
     *
     * @param inventory
     * @return
     */
    public ElongHotelInventoryPO getPoByInventory(Inventory inventory) {
        ElongHotelInventoryPO po = new ElongHotelInventoryPO();
        try {
            BeanUtils.copyProperties(inventory, po);
            po.setUpdateTime(new Date());
            //po.setRandom(IdGeneratorUtil.generateTen());
            po.setHotelId(inventory.getHotelID());
            po.setRoomTypeId(inventory.getRoomTypeId());
            if (inventory.isStatus()) {
                po.setStatus(1);
            } else {
                po.setStatus(0);
            }
            if (StringUtils.isEmpty(inventory.getICBeginTime())) {
                po.setIcBeginTime("");
            }
            if (StringUtils.isEmpty(inventory.getICEndTime())) {
                po.setIcEndTime("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return po;
    }

    /**
     * Inventory转换ElongHotelInventoryPO
     *
     * @param inventory
     * @return
     */
    public ELaElongHotelInvDTO getElaDtoByInventory(Inventory inventory) {
        ELaElongHotelInvDTO dto = new ELaElongHotelInvDTO();
        try {
            BeanUtils.copyProperties(inventory, dto);
            StringBuilder idBuilder = new StringBuilder();
            idBuilder.append(inventory.getHotelID()).append("_")
                    .append(inventory.getRoomTypeId()).append("_")
                    .append(inventory.getHotelCode()).append("_")
                    .append(DateUtil.format(inventory.getDate(), DateUtil.PATTERN_CLASSICAL_SIMPLE));
            dto.setId(idBuilder.toString());
            dto.setUpdateTime(new Date());
            //po.setRandom(IdGeneratorUtil.generateTen());
            dto.setHotelId(inventory.getHotelID());
            dto.setRoomTypeId(inventory.getRoomTypeId());
            if (inventory.isStatus()) {
                dto.setStatus(1);
            } else {
                dto.setStatus(0);
            }
            if (StringUtils.isEmpty(inventory.getICBeginTime())) {
                dto.setIcBeginTime("");
            }
            if (StringUtils.isEmpty(inventory.getICEndTime())) {
                dto.setIcEndTime("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dto;
    }

    /**
     * 缓存库存增量参数
     *
     * @param incrInvLastId 最后更新id
     * @param updateTime    最后更新时间
     */
    public void updateIncrLastIdInfo(String incrInvLastId, Date updateTime) {
        redisTemplate.opsForValue().set(RedisConstant.REDIS_INCR_INV_LASTID_KEY, incrInvLastId);
        redisTemplate.opsForValue().set(RedisConstant.REDIS_INCR_INV_UPDATE_TIME_KEY, updateTime);
    }

    public boolean hasCacheKey(ElongHotelInventoryPO invPo) {
        StringBuilder sb = new StringBuilder();
        sb.append(invPo.getHotelId()).append("_")
                .append(invPo.getRoomTypeId()).append("_")
                .append(invPo.getHotelCode()).append("_")
                .append(DateUtil.SDF_PATTERN_CLASSICAL_SIMPLE.format(invPo.getDate()));
        if (redisTemplate.hasKey(sb.toString())) {
            return true;
        } else {
            return false;
        }
    }

    public void cacheIndex(ElongHotelInventoryPO invPo) {
        StringBuilder sb = new StringBuilder();
        sb.append(invPo.getHotelId()).append("_")
                .append(invPo.getRoomTypeId()).append("_")
                .append(invPo.getHotelCode()).append("_")
                .append(DateUtil.SDF_PATTERN_CLASSICAL_SIMPLE.format(invPo.getDate()));
        if (!redisTemplate.hasKey(sb.toString())) {
            redisTemplate.opsForValue().set(sb.toString(), 1);
        }
    }
}
